package com.ea.controller.front;

import com.ea.api.controller.user.OrderInfoApi;
import com.ea.common.enums.OperationResultCode;
import com.ea.common.exception.ServiceException;
import com.ea.common.response.JsonResult;
import com.ea.common.util.TypeConversionToJson;
import com.ea.domain.dto.OrderCreateDTO;
import com.ea.domain.dto.PayBackInfo;
import com.ea.domain.dto.StoreEvaluateDTO;
import com.ea.domain.entity.OrderFormDetail;
import com.ea.domain.query.OrderInfoQueryDTO;
import com.ea.domain.vo.OrderCreateVO;
import com.ea.domain.vo.OrderDetailVO;
import com.ea.domain.vo.OrderInfoVO;
import com.ea.service.IOrderInfoService;
import com.ea.service.IStoreEvaluateInfoService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单信息主表 前端控制器
 * </p>
 *
 * @author 电器商城
 * @since 2022-11-29
 */
@RestController
@RequestMapping("/electric/front/order")
public class OrderInfoController implements OrderInfoApi {

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IStoreEvaluateInfoService evaluateInfoService;
    @Override
    @PostMapping("/create/info")
    public JsonResult createOrderInfo(@RequestBody String createJson) {
        if (StringUtils.isEmpty(createJson)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        OrderCreateDTO createDTO = TypeConversionToJson.jsonToEntity(createJson, OrderCreateDTO.class);
        if (createDTO == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        OrderCreateVO order = orderInfoService.createOrder(createDTO);
        return JsonResult.doSuccessful(order);
    }

    @Override
    @GetMapping("/change/memo")
    public JsonResult updateOrderInfo(@RequestParam("orderId") Long orderId, @RequestParam("buyerMemo") String buyerMemo) {
        if (orderId == null || StringUtils.isEmpty(buyerMemo)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderInfoService.updateOrderInfo(orderId, buyerMemo);
        return JsonResult.updateSuccessFul();
    }

    @Override
    @PostMapping("/query/owner")
    public JsonResult queryOwnOrderInfo(@RequestBody String queryJson) {
        if (StringUtils.isEmpty(queryJson)) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        OrderInfoQueryDTO queryDTO = TypeConversionToJson.jsonToEntity(queryJson, OrderInfoQueryDTO.class);
        PageInfo<OrderInfoVO> orderInfoList = orderInfoService.listUserOrderInfo(queryDTO);
        return JsonResult.querySuccessFul(orderInfoList);
    }

    @Override
    @GetMapping("/get/detail")
    public JsonResult getOrderDetailInfo(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<OrderFormDetail> detailList = orderInfoService.listOrderDetail(orderId);
        return JsonResult.querySuccessFul(detailList);
    }

    @Override
    @GetMapping("/pay")
    public JsonResult payOrder(@RequestParam("orderId")Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderInfoService.payOrder(orderId);
        return JsonResult.doSuccessful();
    }


    @Override
    @GetMapping("/confirm/order")
    public JsonResult confirmReceiptOrder(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        orderInfoService.confirmOrder(orderId);
        return JsonResult.updateSuccessFul();
    }



    @Override
    @GetMapping("/can/evaluate")
    public JsonResult listCanEvaluateGoods(@RequestParam("orderId")Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        List<OrderDetailVO> detailVOList = evaluateInfoService.listCanEvaluateGoods(orderId);
        return JsonResult.doSuccessful(detailVOList);
    }

    @Override
    @PostMapping("/create/evaluate")
    public JsonResult createEvaluateGoods(@RequestBody StoreEvaluateDTO evaluateDTO) {
        if (evaluateDTO == null || evaluateDTO.getOrderId() == null || evaluateDTO.getSkuId() == null || evaluateDTO.getGoodsId() == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        evaluateInfoService.evaluateGoodsBySkuId(evaluateDTO);
        return JsonResult.doSuccessful();
    }


    @GetMapping("/info/return")
    public JsonResult queryOrder(@RequestParam("orderId") Long orderId) {
        if (orderId == null) {
            throw new ServiceException(OperationResultCode.PARAMETER_ERROR);
        }
        PayBackInfo payBackInfo = orderInfoService.getPayBackInfo(orderId);
        return JsonResult.doSuccessful(payBackInfo);
    }

    @Override
    @PostMapping("/create/repair")
    public JsonResult createRepairForm() {
        return null;
    }
}
